package com.example.mine_platform.service.impl;

import com.example.mine_platform.commom.Result;
import com.example.mine_platform.mapper.DevicesMapper;
import com.example.mine_platform.pojo.dto.DeviceDTO;
import com.example.mine_platform.pojo.dto.DeviceNewDTO;
import com.example.mine_platform.pojo.entities.Devices;
import com.example.mine_platform.pojo.vo.DeviceVO;
import com.example.mine_platform.service.DeviceService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DevicesMapper devicesMapper;

    @Override
    public List<DeviceVO> getAllDevices() {
        List<Devices> devicesList = devicesMapper.getAllMaterials();
        List<DeviceVO> deviceVOList = new ArrayList<>();
        for (Devices x : devicesList) {
            DeviceVO deviceVO = new DeviceVO();
            deviceVO.setId(x.getId());
            deviceVO.setName(x.getName());
            deviceVO.setAmount(x.getAmount());
            deviceVO.setProduce_date(x.getProduceDate());
            deviceVO.setCondition(x.getCondition());
            deviceVOList.add(deviceVO);
        }
        return deviceVOList;
    }

    @Override
    public Devices getDevicesById(Integer id) {
        Devices devices = devicesMapper.selectByPrimaryKey(id);
        return devices;
    }

    @Override
    public Result insertDevice(DeviceNewDTO deviceNewDTO) {
        Devices devices = new Devices();
        devices.setName(deviceNewDTO.getName());
        devices.setDescribe(deviceNewDTO.getDescribe());
        devices.setAmount(deviceNewDTO.getAmount());
        devices.setProduceDate(deviceNewDTO.getProduce_date());
        devices.setCondition(deviceNewDTO.getCondition());
        devicesMapper.insertSelective(devices);
        return Result.success();
    }

    @Override
    public Result updateDevice(DeviceDTO deviceDTO) {
        Integer id = deviceDTO.getId();
        Devices devices = devicesMapper.selectByPrimaryKey(id);
        devices.setName(deviceDTO.getName());
        devices.setDescribe(deviceDTO.getDescribe());
        devices.setAmount(deviceDTO.getAmount());
        devices.setProduceDate(deviceDTO.getProduce_date());
        devices.setCondition(deviceDTO.getCondition());
        devicesMapper.updateByPrimaryKey(devices);
        return Result.success();
    }

    @Override
    public Result deleteDevice(Integer id) {
        Devices devices = devicesMapper.selectByPrimaryKey(id);
        devices.setDeleted((byte) 1);
        devicesMapper.updateByPrimaryKey(devices);
        return Result.success();
    }
}
